import {
  LOGIN,
  ROUTES,
  LOGIN_VERIFI_CODE,
  LOGIN_SEND_MSG,
  LOGIN_CHECK_CODE,
  LOGIN_FORGOT,
  FIRST_LOGIN_SENDMSG
} from '@/services/api'
import { request, METHOD, removeAuthorization } from '@/utils/request'

/**
 * 登录服务
 * @param name 账户名
 * @param password 账户密码
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function login (username, password, securityCode, key) {
  return request(LOGIN, METHOD.POST, 'username=' + username + '&password=' + password)
}

/**
 * 忘记密码，发送验证码
 */
export async function sendMsg (params) {
  return request(LOGIN_SEND_MSG, METHOD.GET, params)
}

/**
 * 忘记密码，检查验证码
 */
export async function checkCode (params) {
  return request(LOGIN_CHECK_CODE, METHOD.POST, params)
}

/**
 * 修改密码
 */
export async function forgot (params) {
  return request(LOGIN_FORGOT, METHOD.POST, params)
}

/**
 * 获取登录验证码
 */
export function getLoginVerifCode () {
  return request(LOGIN_VERIFI_CODE, METHOD.GET)
}

/**
 * 首次登录时，验证手机号
 */
export function firstLoginSendMsg (tel) {
  return request(FIRST_LOGIN_SENDMSG, METHOD.GET, { tel })
}

export async function getRoutesConfig () {
  return request(ROUTES, METHOD.GET)
}

/**
 * 退出登录
 */
export function logout () {
  localStorage.removeItem(process.env.VUE_APP_LOCK_PWD)
  localStorage.removeItem(process.env.VUE_APP_ROUTES_KEY)
  localStorage.removeItem(process.env.VUE_APP_USER_KEY)
  localStorage.removeItem(process.env.VUE_APP_PERMISSIONS_KEY)
  localStorage.removeItem(process.env.VUE_APP_ROLES_KEY)
  localStorage.removeItem(process.env.VUE_APP_MENUS_KEY)
  removeAuthorization()
}
export default {
  login,
  logout,
  getRoutesConfig
}
